<?php

namespace App\Dao\Order;

use App\Model\Model;
use App\Model\Order\StoreOrder;
use App\Model\User;
use Yj\Daos\BaseDao;
use App\Tool\Collect;
use Hyperf\Database\Model\Builder;
use Yj\Daos\BaseDaos;
use Yj\Daos\Verify;

/**
 * @var StoreOrder
 * @method $this payType($params)
 * @method $this status($params)
 * @method $this keywords($params)
 */
class StoreOrderDao extends BaseDaos
{

    public function searchStatus(Collect $params)
    {
        $search = $this->getSearch();
        $params->check(['status', 'keywords'], function ($status, $keywords) use ($search) {
            
            switch ((int)$status) {
                case 0://未支付
                    $search->where('paid', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
                    break;
                case 1://已支付 未发货
                    $search->where('paid', 1)->whereIn('status', [0, 4])->whereIn('refund_status', [0, 3])->when(isset($where['shipping_type']), function ($search) {
                        $search->where('shipping_type', 1);
                    })->where('is_del', 0);
                    break;
                case 7://已支付 部分发货
                    $search->where('paid', 1)->where('status', 4)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 2://已支付  待收货
                    $search->where('paid', 1)->where('status', 1)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 3:// 已支付  已收货  待评价
                    $search->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 4:// 交易完成
                    $search->where('paid', 1)->where('status', 3)->whereIn('refund_status', [0, 3])->where('is_del', 0);
                    break;
                case 5://已支付  待核销
                    $search->where('paid', 1)->where('status', 0)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
                    break;
                case 6://已支付 已核销 没有退款
                    $search->where('paid', 1)->where('status', 2)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
                    break;
                case -1://退款中
                    $search->where('paid', 1)->whereIn('refund_status', [1, 4])->where('is_del', 0);
                    break;
                case -2://已退款
                    $search->where('paid', 1)->where('refund_status', 2)->where('is_del', 0);
                    break;
                case -3://退款
                    $search->where('paid', 1)->whereIn('refund_status', [1, 2, 4])->where('is_del', 0);
                    break;
                case -4://已删除
                    $search->where('is_del', 1);
                    break;
            }
        });
        return $search;
    }

    public function searchPayType(Collect $params)
    {
        $search = $this->getSearch();
        $params->check('pay_type', function ($pay_type) use ($search) {
            switch ($pay_type) {
                case 1:
                    $search->where('pay_type', 'weixin');
                    break;
                case 2:
                    $search->where('pay_type', 'yue');
                    break;
                case 3:
                    $search->where('pay_type', 'offline');
                    break;
                case 4:
                    $search->where('pay_type', 'alipay');
                    break;
            }
        });
        return $search;
    }

    public function searchKeywords($params)
    {
        $search = $this->getSearch();
        $params->check('keywords', function ($keywords) use ($search) {
            $search->where('order_id', 'like', '%' . $keywords . '%');
        });
        return $search;
    }

    public function staticsSales(string|null $time)
    {
        return $this->tap(function($query) use ($time){
            $time && $query->yjRangeTime('add_time', $time);
        })->sum('pay_price');
    }

    public function lists($params)
    {
        $data = $this->keywords($params)->status($params)->payType($params)->with(['User', 'StoreOrderCartInfo'])->orderByDesc('id')->getList();
        return $data;
    }

    public function getMonthCount()
    {
        return $this->yjRangeTime('add_time', 'month')->where('paid', 1)->count();
    }


}